home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / usenet / vmsnet / ttt / part01
Encoding:
Internet Message Format  |  1991-12-20  |  6.9 KB

  1. Path: uunet!caen!uakari.primate.wisc.edu!zazen!mis.mcw.edu!tenaglia
  2. From: tenaglia@mis.mcw.edu
  3. Newsgroups: vmsnet.sources.games
  4. Subject: TICK TACK TOE (1/1)
  5. Message-ID: <1991Dec20.100054.122@mis.mcw.edu>
  6. Date: 20 Dec 91 15:00:54 GMT
  7. Organization: Medical College of Wisconsin MIS Department
  8. Lines: 257
  9.  
  10.  
  11. Enclosed is the source for a really dumb version of Tick Tack Toe. It's
  12. written in the Icon Programming language which is public domain and
  13. available for VMS, unix, dos, amiga, IBM MAINFRAM, MACintosh, etc,...
  14. from the University of Arizona (Icon-Project@cs.arizona,edu).
  15.  
  16. Why am I posting a 'dumb version'? Because it wins more often than it
  17. deserves to (it cheats), and there's no 'explicit' logic implementing
  18. the strategy. The strategy is isomorphic, a side effect of slightly
  19. careless/sloppy coding. I've tested it under VMS and Ultrix unix. I
  20. thought it might be interesting for some. You'll need a VT100 compatible
  21. display. I've been flamed in the past for this, oh well.
  22.  
  23. Chris Tenaglia (System Manager) |  "The past explained,     
  24. Medical College of Wisconsin    |   the future fortold, 
  25. 8701 W. Watertown Plank Rd.     |   the present largely appologized for."
  26. Milwaukee, WI 53226             |   Organon to The Doctor
  27. (414)257-8765                   |     
  28. tenaglia@mis.mcw.edu
  29.  
  30.  
  31. ###############################################################
  32. #                                                             #
  33. #          file : ttt.icn                                     #
  34. #          updt : 27-nov-1991                                 #
  35. #          auth : chris tenaglia                              #
  36. #          desc : tictactoe icon implementation               #
  37. #          note : This version cheats because of sloppy use   #
  38. #                 of string and numeric compares. It actually #
  39. #                 is more fun this way. Save for posterity.   #
  40. #                                                             #
  41. ###############################################################
  42. global me,you,true,false,draw,pointer,wins,pass,taken,winner,mark,row
  43. procedure main()
  44.   init()
  45.   play := true
  46.   while play == true do
  47.     {
  48.     me      := set()      # computer is me
  49.     you     := set()      # player   is you
  50.     victory := ""         # nobodys' won yet
  51.     pass    := 0          # start flag
  52.     winner  := ""
  53.     taken   := table(false)    # taken position table (rather than set?)
  54.     display()
  55.     insert(me,5)
  56.     taken[5] := true
  57.     display()
  58.     insert(you,(tmp := get_your_move()))
  59.     taken[integer(tmp)] := true
  60.     display()
  61.     repeat
  62.       {
  63.       insert(me,(tmp := choose([1,2,3,4,6,7,8,9])))
  64.       taken[integer(tmp)] := true
  65.       display()
  66.       if (victory := done_yet()) == (true|false|draw) then break
  67.       insert(you,(tmp := get_your_move()))
  68.       taken[integer(tmp)] := true
  69.       if (victory := done_yet()) == (true|false|draw) then break
  70.       }
  71.     display()
  72.     case winner of
  73.       {
  74.       "me" : {
  75.              write(at(1,22),chop(&host)," Wins, You Loose!")
  76.              every square := !row do writes(pointer[square],mark)
  77.              }
  78.       "you": {
  79.              write(at(1,22),chop(),"Wow! You actually beat the Computer.")
  80.              every square := !row do writes(pointer[square],mark)
  81.              } 
  82.       draw : write(at(1,22),chop(),"Game was a draw.")
  83.       }
  84.     play := map(input(at(1,23) || "Another Game (Y/N) :")[1])
  85.     }
  86.   end
  87.  
  88. #
  89. # procedure to display the current tictactoe grid and plays
  90. #
  91. procedure display()
  92.   if (pass +:= 1) = 1 then
  93.     {
  94.     write(cls(),uhalf(),"          T I C - T A C - T O E")
  95.     write(lhalf(),"          T I C - T A C - T O E")
  96.     write(trim(center("Computer is 'O' and you are 'X'",80)))
  97.     line := repl("q",60) ; line[21] := "n" ; line[41] := "n"
  98.     every y := 5 to 20 do writes(at(30,y),graf("x"))
  99.     every y := 5 to 20 do writes(at(50,y),graf("x"))
  100.     writes(at(10,10),graf(line))
  101.     writes(at(10,15),graf(line))
  102.     every x := 1 to 9  do writes(pointer[x],dim(x))
  103.     }
  104.   every writes(pointer[!me],high("O"))
  105.   every writes(pointer[!you],under("X"))
  106.   end
  107.  
  108. #
  109. # procedure to obtain a move choice from the player
  110. #
  111. procedure get_your_move()
  112.   local yours,all_moves
  113.   repeat {
  114.   writes(at(5,22))
  115.   yours := input("Enter block # (1-9) :")
  116.   writes(at(5,23),chop())
  117.   if not(integer(yours)) then
  118.     {
  119.     writes(at(5,23),beep(),"Invalid Input! Choose 1-9.")
  120.     next
  121.     }
  122.   if (1 > yours) | (yours > 9) then
  123.     {
  124.     writes(at(5,23),beep(),"Value out of range! Choose 1-9.")
  125.     next
  126.     }
  127.   if taken[integer(yours)] == true then
  128.     {
  129.     writes(at(5,23),beep(),"That position is already taken! Try again.")
  130.     next
  131.     }
  132.   break }
  133.   return yours
  134.   end
  135.  
  136. #
  137. # procedure chooses for the host a strong move
  138. #
  139. procedure choose(lst)
  140.   local val,test
  141.   test := 0
  142.   repeat {
  143.   val := ?lst
  144.   if (test +:= 1) > 200 then write(val," Choose: infinite loop")
  145.   if taken[integer(val)] == true then next else break }
  146.   return val
  147.   end
  148.   
  149. #
  150. # procedure to test if computer has won, or the game is a draw
  151. #
  152. procedure done_yet()
  153.   every outcome := !wins do
  154.     {
  155.     test := 0
  156.     every part := !outcome do
  157.       if member(you,part) then test +:= 1
  158.     if test = 3 then
  159.       {
  160.       winner := "you"
  161.       row    := outcome
  162.       mark   := high(blink("X"))
  163.       return true
  164.       }
  165.     }
  166.   every outcome := !wins do
  167.     {
  168.     test := 0
  169.     every part := !outcome do
  170.       if member(me,part) then test +:= 1
  171.     if test = 3 then
  172.       {
  173.       winner := "me"
  174.       row    := outcome
  175.       mark   := high(blink("O"))
  176.       return true
  177.       }             
  178.     }
  179.   if *me + *you > 8 then
  180.     {
  181.     winner := draw
  182.     return draw
  183.     }
  184.   return "not done yet"
  185.   end
  186.  
  187. #
  188. # prompts for an input from the user
  189. #
  190. procedure input(prompt)
  191.   writes(prompt)
  192.   return read()
  193.   end
  194.  
  195. #
  196. # procedures to output ansi graphics and attributes
  197. #
  198. procedure at(x,y)
  199.   return "\e[" || y || ";" || x || "f"
  200.   end
  201.   
  202. procedure graf(str)
  203.   return "\e(0" || str || "\e(B"
  204.   end
  205.  
  206. procedure uhalf(str)
  207.   /str := ""
  208.   return "\e#3" || str
  209.   end
  210.  
  211. procedure lhalf(str)
  212.   /str := ""
  213.   return "\e#4" || str
  214.   end
  215.  
  216. procedure high(str)
  217.   return "\e[1m" || str || "\e[0m"
  218.   end
  219.  
  220. procedure normal(str)
  221.   return "\e[0m" || str
  222.   end
  223.  
  224. procedure dim(str)
  225.   return "\e[2m" || str || "\e[0m"
  226.   end
  227.   
  228. procedure under(str)
  229.   return "\e[4m" || str || "\e[0m"
  230.   end
  231.  
  232. procedure blink(str)
  233.   return "\e[5m" || str || "\e[0m"
  234.   end
  235.  
  236. procedure cls(str)
  237.   /str := ""
  238.   return "\e[2J\e[H" || str
  239.   end
  240.  
  241. procedure chop(str)
  242.   /str := ""
  243.   return "\e[J" || str
  244.   end
  245.  
  246. procedure beep()
  247.   return "\7"
  248.   end
  249.   
  250. #
  251. # procedure to init useful global variables for later use
  252. #
  253. procedure init()
  254.   true    := "y"
  255.   false   := "n"
  256.   draw    := "?"
  257.   &random := map(&clock,":","0")
  258.   wins    := [set([1,5,9]),set([3,5,7]),set([1,2,3]),set([4,5,6]),
  259.               set([7,8,9]),set([1,4,7]),set([2,5,8]),set([3,6,9])]
  260.   pointer := [at(17,7), at(37,7), at(57,7),
  261.               at(17,12),at(37,12),at(57,12),
  262.               at(17,17),at(37,17),at(57,17)]
  263.   end
  264.   
  265.               
  266.                                                     
  267.